import java.awt.GridBagLayout;
import javax.swing.JPanel;
import java.awt.Dimension;
import javax.swing.JButton;
import java.awt.GridBagConstraints;
import java.awt.Rectangle;
import javax.swing.JTextField;
import java.awt.FlowLayout;
import java.awt.Insets;
import javax.swing.BoxLayout;

import netscape.javascript.JSObject;

import java.awt.GridLayout;
import java.io.UnsupportedEncodingException;
import java.security.NoSuchAlgorithmException;

public class ControlPanel extends JPanel {

	private static final long serialVersionUID = 1L;
	private JButton buttonClear = null;
	private JButton buttonSubmitPassword = null;
	private JButton buttonSeePassword = null;
	private JTextField fieldShowPassword = null;
	private JPanel panel1 = null;
	private JPanel panel2 = null;
	
	protected String mode = "not changed";  //  @jve:decl-index=0:
	
	JSObject win;
	
	protected  DoodleCanvas canvas;
	protected PasswordGen password;
	private RunScript rs = new RunScript();

	/**
	 * This is the default constructor
	 */
	public ControlPanel(DoodleCanvas canvas, PasswordGen password, JSObject win) {
		super();
		this.canvas=canvas;
		this.password=password;
		this.win = win;
		initialize();
	}

	public ControlPanel(DoodleCanvas canvas, PasswordGen password) {
		super();
		this.canvas=canvas;
		this.password=password;
		initialize();
	}
	
	
	
	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	

	private void initialize() {
		GridLayout gridLayout = new GridLayout();
		//gridLayout.setRows(2);
		//this.setLayout(gridLayout);
		//this.setSize(430, 139);
		this.add(getPanel1(), null);
		//this.add(getPanel2(), null);
	}

	/**
	 * This method initializes buttonClear	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getButtonClear() {
		if (buttonClear == null) {
			buttonClear = new JButton();
			buttonClear.setText("Clear");
			buttonClear.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					canvas.clearCanvas();
					canvas.removeElementsVectors();
					password.clearPassword();
					//fieldShowPassword.setText(password.getPassword());
					canvas.repaint();
					//System.out.println("actionPerformed()"); // TODO Auto-generated Event stub actionPerformed()
					
					//log CLEAR event
			        rs.runScript("../logdata.php?system=" + rs.system + "&user=" + rs.username + "&scheme=das" + "&mode=" + mode
					           + "&event=clear");
				}
			});
		}
		return buttonClear;
	}

	/**
	 * This method initializes buttonSubmitPassword	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getButtonSubmitPassword() {
		if (buttonSubmitPassword == null) {
			buttonSubmitPassword = new JButton();
			buttonSubmitPassword.setText("Submit");
			buttonSubmitPassword.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					//fieldShowPassword.setText("oi bruna");
					String pass = password.getPassword();
//
//			        try {
//			            //System.out.println("MD5 hash of string: " + AeSimpleMD5.MD5(pass));
//			        	pass = AeSimpleMD5.MD5(pass);
//			        } catch (NoSuchAlgorithmException ex) {
//			            // TODO Auto-generated catch block
//			            ex.printStackTrace();
//			        } catch (UnsupportedEncodingException ex) {
//			            // TODO Auto-generated catch block
//			            ex.printStackTrace();
//			        }
//					
					
					submitData(pass);
					
				}
			});
		}
		return buttonSubmitPassword;
	}
	
	
	   private void submitData(String data){
	       String[] submitArgs = new String[2];
	       submitArgs[0] = data; //password?
	       submitArgs[1] = mode;// "Draw-a-Secret"; //mode
	       win.call("passresult", submitArgs);
	   }
	
		public void setMode(String mode){
			this.mode=mode;
		}
		
	   
	/**
	 * This method initializes buttonSeePassword	
	 * 	
	 * @return javax.swing.JButton	
	 */
//	private JButton getButtonSeePassword() {
//		if (buttonSeePassword == null) {
//			buttonSeePassword = new JButton();
//			buttonSeePassword.setText("See Password");
//			buttonSeePassword.addActionListener(new java.awt.event.ActionListener() {
//				public void actionPerformed(java.awt.event.ActionEvent e) {
//					try{
//					fieldShowPassword.setText(password.getPassword());
//					//System.out.println("actionPerformed()"); // TODO Auto-generated Event stub actionPerformed()
//					}catch (Exception exp){
//						System.err.print(exp);
//					}
//					
//					
//				}
//			});
//		}
//		return buttonSeePassword;
//	}

	/**
	 * This method initializes fieldShowPassword	
	 * 	
	 * @return javax.swing.JTextField	
	 */
//	private JTextField getFieldShowPassword() {
//		if (fieldShowPassword == null) {
//			fieldShowPassword = new JTextField();
//			fieldShowPassword.setPreferredSize(new Dimension(120, 20));
//		}
//		return fieldShowPassword;
//	}

	/**
	 * This method initializes panel1	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getPanel1() {
		if (panel1 == null) {
			panel1 = new JPanel();
			panel1.setLayout(new FlowLayout());
			panel1.add(getButtonClear(), null);
			panel1.add(getButtonSubmitPassword(), null);
		}
		return panel1;
	}

	/**
	 * This method initializes panel2	
	 * 	
	 * @return javax.swing.JPanel	
	 */
//	private JPanel getPanel2() {
//		if (panel2 == null) {
//			panel2 = new JPanel();
//			panel2.setLayout(new FlowLayout());
//			panel2.add(getButtonSeePassword(), null);
//			panel2.add(getFieldShowPassword(), null);
//		}
//		return panel2;
//	}

	
	public void setRunScript(RunScript rs){
		this.rs=rs;
	}
	
	
}  //  @jve:decl-index=0:visual-constraint="76,24"
